package br.sc.senai.view;

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComboBox;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JScrollPane;

import javax.swing.Box;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.ImageIcon;

import br.sc.senai.dao.VendaDAO;
import br.sc.senai.model.ForcarSelecaoUnica;
import br.sc.senai.model.Venda;
import br.sc.senai.model.VendaTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;

@SuppressWarnings("serial")
public class ConsultarVendasUI extends JInternalFrame {
	private JTable table = new JTable();
	private VendaDAO vendaDAO = new VendaDAO();
	private ArrayList<Venda> vendas = vendaDAO.consultar();

	private static ConsultarVendasUI instancia;

	public static ConsultarVendasUI obterInstancia() {
		if (instancia == null) {
			instancia = new ConsultarVendasUI();
		}
		return instancia;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					ConsultarVendasUI frame = new ConsultarVendasUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ConsultarVendasUI() {
		setTitle("Consultar Vendas");
		Dimension d = PrincipalUI.obterInstancia().getSize();
		d.setSize(d.getWidth(), (d.getHeight() - 100));
		setBounds(0, 80, (int) d.getWidth(), (int) d.getHeight());
		this.setMinimumSize(d);

		addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseMoved(MouseEvent e) {
				// setLocation(0, 80);
			}
		});
		setClosable(true);
		setResizable(false);

		final JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Intervalo de Datas", "Nome do Cliente", "Nome do Funcion\u00E1rio"}));

		JScrollPane scrollPane = new JScrollPane();

		Box horizontalBox = Box.createHorizontalBox();

		JButton btnIncluir = new JButton("Incluir");
		btnIncluir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				IncluirClienteUI incluirClienteUI = IncluirClienteUI
						.obterInstancia();
				incluirClienteUI.moveToFront();
				incluirClienteUI.setFocusable(true);
				incluirClienteUI.requestFocus();
				PrincipalUI.obterInstancia().getContentPane()
						.add(incluirClienteUI, 0);
				incluirClienteUI.setVisible(true);
			}
		});
		btnIncluir.setIcon(new ImageIcon(ConsultarVendasUI.class
				.getResource("/br/sc/senai/icones/Check_24x24.png")));

		JButton btnNewButton = new JButton("Editar");
		btnNewButton.setIcon(new ImageIcon(ConsultarVendasUI.class
				.getResource("/br/sc/senai/icones/editar.png")));
		JButton btnExcluir = new JButton("Excluir");
		btnExcluir.setIcon(new ImageIcon(ConsultarVendasUI.class
				.getResource("/br/sc/senai/icones/Delete_24x24.png")));

		JButton btnSaor = new JButton("Fechar");
		btnSaor.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnSaor.setIcon(new ImageIcon(ConsultarVendasUI.class
				.getResource("/br/sc/senai/icones/Undo_24x24.png")));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(btnIncluir)
					.addGap(18)
					.addComponent(btnNewButton)
					.addGap(18)
					.addComponent(btnExcluir)
					.addPreferredGap(ComponentPlacement.RELATED, 528, Short.MAX_VALUE)
					.addComponent(btnSaor)
					.addContainerGap())
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(23)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 973, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 221, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 752, Short.MAX_VALUE)
							.addComponent(horizontalBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(18))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(horizontalBox, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE))
					.addGap(12)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 618, Short.MAX_VALUE)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnIncluir)
						.addComponent(btnNewButton)
						.addComponent(btnExcluir)
						.addComponent(btnSaor))
					.addContainerGap())
		);

		table.setSelectionModel(new ForcarSelecaoUnica());
		table.setModel(new VendaTableModel(vendas));
		table.getTableHeader().setResizingAllowed(false);
		table.getTableHeader().setReorderingAllowed(false);
		scrollPane.setViewportView(table);
		getContentPane().setLayout(groupLayout);

	}
}
